﻿using Common;
using MySql.Data.MySqlClient;
using NB.MGJ.FrameWork;
using NB.MGJ.Model;
using NB.MGJ.Service.AntiFake;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Service.CodeManage
{
    /// <summary>
    /// 互码识别
    /// </summary>
    public class DigitcodeService
    {
        public QueryStatisticsService _qsService = new QueryStatisticsService();
        public BaseRepository _baseRepository = new BaseRepository();
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pager"></param>
        /// <returns></returns>
        public PagedList<AntiFake_Digitcode> GetPagerList(ref PagerModel pager, string code, int eid, int allotcodes)
        {
           
            StringBuilder strSql1 = new StringBuilder();
            
            strSql1.Append(" ");
            try
            {
                strSql1.Append("a.ID,");
                strSql1.Append("a.Fw_CodeSecret,");
                strSql1.Append("a.Wl_Code,");
                strSql1.Append("a.WB_Fw_Code,");
                strSql1.Append("a.WB_Wl_Code,");
                strSql1.Append("a.EnterpriseId,");
                strSql1.Append("a.Flag,");
                strSql1.Append("a.Stats,");
                strSql1.Append("a.QueryCount,");
                strSql1.Append("a.QueryTime,");
                strSql1.Append("a.CreateDate,");
                strSql1.Append("a.ChangeDate,");
                strSql1.Append("a.ProductId,");
                strSql1.Append("a.CodeType,");
                strSql1.Append("e.EnterpriseName,");
                strSql1.Append("e.Code,");
                strSql1.Append("e.PersonMobile,");
               //strSql1.Append("e.BoxFW_Code,");
               //strSql1.Append("a.ParentCode,");
                var strSql = new StringBuilder(strSql1.ToString().Trim(','));
                pager.FldName = strSql.ToString();
                pager.TblName = " nbcodemain.AntiFake_Digitcode" + code + " a, nbsecuritymain.Organization_Enterprise e ";

              
                if (string.IsNullOrEmpty(pager.StrCondition))
                    pager.StrCondition = "  a.EnterpriseId=e.ID and e.ViewFlag=1 and  a.EnterpriseId=" + allotcodes;
                else
                {

                    pager.StrCondition += " and e.ViewFlag=1 and  a.EnterpriseId=e.ID  and a.Stats=0 and  a.EnterpriseId=" + allotcodes;
                    
                }
                if (string.IsNullOrEmpty(pager.FldSort) || pager.FldSort == " ")
                    pager.FldSort = " a.ID desc ";

                MySqlParameter[] parameters= _baseRepository.SqParameterRefactor(ref pager);
                IEnumerable<AntiFake_Digitcode> resultList = _baseRepository.ExecuteStoredProcedureCollection<AntiFake_Digitcode>("Common_PageList", parameters);
                return new PagedList<AntiFake_Digitcode>(resultList, pager.Page, pager.PageSize, 1, Convert.ToInt32(parameters[6].Value));
               
            }
            catch (Exception )
            {

            }
            return null;
        }

        public string allotcode(string where,string code)
        {
            try
            {

                var fcode = code.Substring(0, 8);
                string strSqls = "SELECT EnterpriseId FROM nbcodemain.antifake_digitcode" + fcode + " where "+where+" and Stats=0";
                var alloteid = _baseRepository.ExecuteText<string>(strSqls);
                return alloteid;
            }
            catch
            {
                return null;
            }
        }
        public int Heaid(int eid)
        {


                var sqls = "select HeadId from Organization_Enterprise where Id=" + eid;
                int Heaid = _baseRepository.ExecuteText<int>(sqls);
                return Heaid;
           
        
        }
        public string state(string code)
        {
            try
            {

                string strSql = string.Format("SELECT id from  nbcodemain.antifake_digitcode{0} where  Stats=0", code);
                return _baseRepository.ExecuteText<string>(strSql);
            }
            catch
            {
                return null;
            }
        }
        #region 表是否存在

		/// <summary>
		/// 判断表是否存在
		/// </summary>
        public bool TabExists(string code)
        {
			string _sql = string.Format("SHOW TABLES LIKE  'AntiFake_Digitcode{0}'", code);
			using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["mgj_codesql"].ConnectionString)) {
				using (MySqlCommand cmd = conn.CreateCommand()) {
					conn.OpenAsync();
					cmd.CommandText = _sql;
					cmd.CommandType = System.Data.CommandType.Text;
					var _result = cmd.ExecuteScalar();
					if (_result == null) {
						return false;
					}
					return true;
				}
			}
          //  return _baseRepository.Exists("AntiFake_Digitcode" + code);
		   // string sql="select * from AntiFake_Digitcode"+code;
		
		   // string _sqls = string.Format("SHOW TABLES LIKE  'AntiFake_Digitcode{0}'", 123123123);
		   //  string connstr = ConfigurationManager.ConnectionStrings["mgj_codesql"].ToString();
		   // var obj = _baseRepository.GetTable(sql, connstr);
		   //var rs =		_baseRepository.ExecuteSql<string>(_sql);
		   //var rss = _baseRepository.ExecuteSql<string>(_sqls);

		   //if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
		   //{
		   //	return false;
		   //}
		   //else
		   //{
		   //	return true;
		   //}
        }
        #endregion
    }
}
